原使用http://127.0.0.1/ReportServer?/folder_name/report_name&dt=20240730&rs:Format=PDF
會在CLIENT端下載,但無法指定位置和檔名
這次的方法,可以將檔案下載至SERVER端.
使用方法
string strFileName =@"D:\20240730.PDF";//下載的檔案要放在哪裡,叫什麼名子
string URL ="http://127.0.0.1/ReportServer?/folder_name/report_name&dt=20240730&rs:Format=PDF";
string sID ="id";
string sPwd="pwd";
outFile(strFileName, URL, sID, sPwd);
private void outFile(string strFileName, string URL, string sID, string sPwd)
{
string strMsg = "";
FileStream stream = null;
string encoding = string.Empty;
string extension = string.Empty;
string mimeType = string.Empty;
try
{
#region 下載報表
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(URL);
webrequest.Credentials = new System.Net.NetworkCredential(sID, sPwd);
HttpWebResponse WebResponse = (HttpWebResponse)webrequest.GetResponse();
int MAXSIZE = 0;
MAXSIZE = (int)WebResponse.ContentLength;
System.IO.Stream dataStream = WebResponse.GetResponseStream();
byte[] buffer = new byte[8192];
FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write);
int size = 0;
int _TOTALSIZE = 0;
do
{
size = dataStream.Read(buffer, 0, buffer.Length);
_TOTALSIZE += size;
double svalue = _TOTALSIZE / MAXSIZE * 100;
if (size > 0)
fs.Write(buffer, 0, size);
} while (size > 0);
fs.Close();
WebResponse.Close();
#endregion
}
catch (System.Exception ex)
{
strMsg = ex.ToString();
Response.Write(strMsg);
}
finally
{
if (stream != null)
{
stream.Close();
}
}
}